import {useLocation, useNavigate} from "react-router-dom";
import {useEffect} from "react";
import {authorize} from "../api/authorize.ts";
import {useDispatchLoginState, useDispatchUserToken} from "../stores/hooks/index.ts"; // 假设你有一个对应的 CSS 文件

export default function OnAuthCallback() {
    const navigate = useNavigate();
    const location = useLocation();
    const {setLogin}=useDispatchLoginState();
    const {setUserToken}=useDispatchUserToken();
    // token是后端设置TTL，若过期（token不一致），返回401，响应拦截-》登录页(清除token)
    useEffect(() => {
        const params = new URLSearchParams(location.search);
        const code = params.get('code');
        localStorage.setItem("Auth_code",code)
        const state=localStorage.getItem("Auth_state")
        if (code) {
            authorize(state,code).then(response => {
                localStorage.setItem('Auth_token', response?.data.res);
                setUserToken(response?.data.res);
                setLogin(true);
                navigate('/'); // 跳转首页
            });
        }
    }, [location, navigate]);
    /*TODO:带优化样式*/
    return <div>处理授权中...</div>;
}
